package com.smartfactory.sail.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author 王巨壁
 * 2019/04/12
 **/
public class DateUtils {
    /**
     * 时间格式化字符串
     */
    public static final String time24Formatter = "yyyy-MM-dd HH:mm:ss";
    public static final String dateFormart = "yyyy-MM-dd";
    public static final String timeForFile = "yyyyMMddHHmmss";
    /**
     * 获取当前时间（标准格式）
     */
    public static String getNowTime() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(time24Formatter);
        return dateFormat.format(now);
    }

    /**
     * 获取当前日期（标准格式）
     */
    public static String getNowDate() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormart);
        return dateFormat.format(now);
    }

    /**
     * 获取当前时间无符号的
     */
    public static String getNowTimeNoting() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeForFile);
        return simpleDateFormat.format(date);
    }

    /**
     * 获取当前日期（格式YYYYMMDD）
     */
    public static String getNowDay() {
        Date now = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        return formatter.format(now).substring(0, 8);
    }

    /**
     * 获取当前年份
     * @return
     */
    public static int getNowYear() {
        String nowDay = DateUtils.getNowDay();
        return Integer.parseInt(nowDay.substring(0, 4));
    }

    /**
     * 获取当前月份
     * @return
     */
    public static int getNowMonth() {
        String nowDay = DateUtils.getNowDay();
        return Integer.parseInt(nowDay.substring(4,6));
    }

    /**
     * 获取当前日期号码
     * @return
     */
    public static int getNowDayNu() {
        String nowDay = DateUtils.getNowDay();
        return Integer.parseInt(nowDay.substring(6,8));
    }

    /**
     * 根据给定时间格式转换成字符串
     * @param time
     * @param forMatter
     * @return
     */
    public static String forMatterLocalDateTime(LocalDateTime time, String forMatter) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(forMatter);

        return time.format(formatter); //
    }

    /**
     * 两个日期字符串 相隔的天数
     * @param startDate
     * @param endDate
     * @return
     */
    public static Long dayDiff(String startDate, String endDate) {
        SimpleDateFormat formater = new SimpleDateFormat(dateFormart);
        try {
            long d1 = formater.parse(startDate).getTime();
            long d2 = formater.parse(endDate).getTime();
            //diff=(Math.abs(d1-d2) / (1000 * 60 * 60 * 24));
            return (d2-d1)/(1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取两个日期字符串之间的日期集合
     * @param startTime:String
     * @param endTime:String
     * @return list:yyyy-MM-dd
     */
    public static List<String> getBetweenDate(String startTime, String endTime){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormart);
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<=endDate.getTime()){
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate=calendar.getTime();
            }
        } catch (ParseException e) {

        }
        return list;
    }

    /**
     * 获取两个日期之间所有的月份集合
     * @param startTime
     * @param endTime
     * @return：YYYY-MM
     */
    public static List<String> getMonthBetweenDate(String startTime, String endTime){
        List<String> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        try {

            min.setTime(sdf.parse(startTime));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
            max.setTime(sdf.parse(endTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;

    }

    public static void main(String[] args) {
        String now = "2018-12-01";

        String today = DateUtils.getNowDate();

       Long betweenDate = DateUtils.dayDiff(now, today);

        System.out.println(betweenDate);
    }
}
